package ynu.edu.elmspringboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import ynu.edu.elmspringboot.mapper.BusinessMapper;
import ynu.edu.elmspringboot.po.Business;
import ynu.edu.elmspringboot.service.BusinessService;

import java.util.List;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    
    @Override
    public List<Business> slectBusinessByOrderTypeId(Integer orderTypeId) {
        return this.getBaseMapper().slectBusinessByOrderTypeId(orderTypeId);
    }

    @Override
    public IPage<Business> getBusinessPage(Page<Business> page, String businessName, Integer orderTypeId) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        
        // 添加商家名称模糊查询条件
        if (StringUtils.hasText(businessName)) {
            queryWrapper.like("business_name", businessName);
        }
        
        // 添加商家类型查询条件
        if (orderTypeId != null) {
            queryWrapper.eq("order_type_id", orderTypeId);
        }
        
        // 按商家ID排序
        queryWrapper.orderByAsc("business_id");
        
        return this.page(page, queryWrapper);
    }

    @Override
    public IPage<Business> getBusinessPageByOrderType(Page<Business> page, Integer orderTypeId) {
        QueryWrapper<Business> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_type_id", orderTypeId);
        queryWrapper.orderByAsc("business_id");
        
        return this.page(page, queryWrapper);
    }
}
